﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ACSR.Core.Streams
{
    public static class StreamExtensions
    {
        public static byte[] ReadSizedObject(this Stream s) // Note parameter declaration.
        {
            var buf = new byte[sizeof(Int32)];
            s.Read(buf, 0, buf.Length);
            var size = BitConverter.ToInt32(buf, 0);
            buf = new byte[size];
            s.Read(buf, 0, buf.Length);
            return buf;
        }
        public static void WriteSizedString(this Stream s, string stringToWrite)
        {
           
            var bSize = BitConverter.GetBytes((Int32)stringToWrite.Length);
            s.Write(bSize, 0, bSize.Length);
            var buf = ASCIIEncoding.ASCII.GetBytes(stringToWrite);
            s.Write(buf, 0, buf.Length);
        }
        public static string ReadString(this Stream s, int count)
        {
            var buf = new byte[count];
            s.Read(buf, 0, buf.Length);
            return Encoding.ASCII.GetString(buf) ;
        }
        public static string ReadSizedObjectAsString(this Stream s) // Note parameter declaration.
        {
            return ASCIIEncoding.ASCII.GetString(ReadSizedObject(s));
        }
    }

   
}
